package com.dongrj.framework.starter.sms.client;

import com.dongrj.framework.starter.sms.SmsRequest;
import com.dongrj.framework.starter.sms.SmsResponse;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public abstract class AbstractSmsClient implements SmsClient {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public void sendBefore(SmsRequest smsRequest) {

    }

    @Override
    public SmsResponse send(SmsRequest smsRequest) {
        Assert.hasLength(smsRequest.getMobiles(), "手机号不能为空!");

        boolean b1 = StringUtils.hasText(smsRequest.getTemplateCode());
        boolean b2 = StringUtils.hasText(smsRequest.getContent());
        boolean result = b1 || b2;
        Assert.isTrue(result, "模板编码或者内容不能同时为空!");

        setupCountryAreaCode(smsRequest);
        setupSmsRequest(smsRequest);
        sendBefore(smsRequest);
        long start = System.currentTimeMillis();
        SmsResponse smsResponse = doSend(smsRequest);
        if (smsResponse.isSuccess()) {
            log.info("sendSuccess.smsRequest={},time={}", smsRequest, (System.currentTimeMillis() - start));
            sendSuccess(smsRequest, smsResponse);
        }
        if (smsResponse.isFail()) {
            log.warn("sendFail.smsRequest={},smsResponse={}，time={}", smsRequest, smsResponse, (System.currentTimeMillis() - start));
            sendFail(smsRequest, smsResponse);
        }
        return smsResponse;
    }

    public List<String> mobileToList(String mobiles) {
        String[] split = mobiles.split(",");
        List<String> stringList = Lists.newArrayListWithCapacity(split.length);
        stringList.addAll(Arrays.asList(split));
        return stringList;
    }

    public abstract SmsResponse doSend(SmsRequest smsRequest);

    public void sendSuccess(SmsRequest smsRequest, SmsResponse smsResponse) {

    }

    public void sendFail(SmsRequest smsRequest, SmsResponse smsResponse) {

    }

    public void setupSmsRequest(SmsRequest smsRequest) {

    }

    public boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    public boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public boolean equals(String str1, String str2) {
        if (isEmpty(str1)) {
            return isEmpty(str2);
        } else {
            return str1.equals(str2);
        }
    }

    public boolean notEquals(String str1, String str2) {
        return !equals(str1, str2);
    }


    public <T> int size(Collection<T> collection1) {
        if (collection1 == null || collection1.isEmpty()) {
            return 0;
        }
        return collection1.size();
    }

    public <T> boolean sizeEquals(Collection<T> collection1, Collection<T> collection2) {
        return size(collection1) == size(collection2);
    }

    public void setupCountryAreaCode(SmsRequest smsRequest) {
        if (needCountryCode()) {
            String mobiles = smsRequest.getMobiles();
            List<String> strings = mobileToList(mobiles);
            List<String> list = strings.stream().map(this::addCountryAreaCode).collect(Collectors.toList());
            String newMobiles = Joiner.on(",").join(list);
            smsRequest.setMobiles(newMobiles);
        }
    }

    public String addCountryAreaCode(String mobile) {
        if (!mobile.startsWith("+")) {
            String countryAreaCode = "+86";
            return countryAreaCode + mobile;
        }
        return mobile;
    }

    public <T, R> void doIfEmpty(T oldValue, T newValue, Consumer<T> consumer) {
        if (ObjectUtils.isEmpty(oldValue)) {
            consumer.accept(newValue);
        }
    }

    public boolean needCountryCode() {
        return true;
    }
}
